package com.songshuang.nomina.service.impl;

import com.songshuang.nomina.service.ChatService;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ChatServiceImpl implements ChatService {
    @Override
    public String chat(String prompt) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n  \"model\": \"gpt-3.5-turbo\",\n  \"messages\": [{\"role\": \"user\", \"content\": \" "+prompt+" \"}],\n  \"stream\": false\n}");
        Request request = new Request.Builder()
                .url("https://chatapi.littlewheat.com/v1/chat/completions")
                .method("POST", body)
                .addHeader("Authorization", "Bearer sk-o73Z0GSAKapWTasa1WWISjnhqb1Dq0zIdwlu7N06SFwCn9gi")
                .addHeader("Content-Type", "application/json")
                .build();
        String responseBody = null;
        try {
            responseBody = client.newCall(request).execute().body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return responseBody;
    }

    public static void main(String[] args) throws IOException {
        System.out.println(new ChatServiceImpl().chat("你好"));
    }
}
